﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    [SerializeField] private float attackRange = 3f;

    public Monster CurrentMonsterTarget { get; set; }

    private HeroSkills _heroskills;
    private bool _gameStarted;
    private List<Monster> _monsters;

    private void Start()
    {
        _gameStarted = true;
        _monsters = new List<Monster>();
        _heroskills = GetComponent<HeroSkills>();
    }

    private void Update()
    {
        GetCurrentMonsterTarget();
    }

    private void GetCurrentMonsterTarget()
    {
        if (_monsters.Count <= 0)
        {
            CurrentMonsterTarget = null;
            return;
        }
        CurrentMonsterTarget = _monsters[0];
    }

    private int RotateTowardsTarget()
    {
        if ((CurrentMonsterTarget != null) && (CurrentMonsterTarget.transform.position.y < transform.position.y && _monsters.Count > 0))
        {
            return 1;
        }
        else if ((CurrentMonsterTarget != null) && (CurrentMonsterTarget.transform.position.y >= transform.position.y && _monsters.Count > 0))
        {
            return 2;
        }
        else
        {
            return 3;
        }
    }

    public int GetHeroTowards => RotateTowardsTarget();

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Monster"))
        {
            Monster newMonster = collision.GetComponent<Monster>();
            _monsters.Add(newMonster);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Monster"))
        {
            Monster monster = collision.GetComponent<Monster>();
           if (_monsters.Contains(monster))
            {
                _monsters.Remove(monster);
            }
        }
    }

    private void OnDrawGizmos()
    {
        if (!_gameStarted)
        {
            GetComponent<CircleCollider2D>().radius = attackRange;
        }
        Gizmos.DrawWireSphere(transform.position, attackRange);
    }
}
